import { createRouter } from 'vue-router'
import { history, flatMultiLevelRoutes } from './helper'
import routeSettings from '@/config/route'

const Layouts = () => import('@/layouts/index.vue')

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ':path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/403',
    component: () => import('@/views/error-page/403.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    meta: {
      hidden: true
    },
    alias: '/:pathMatch(.*)*'
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/',
    component: Layouts,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: {
          title: 'dashboard',
          svgIcon: 'dashboard',
          i18nKey: '路由首页',
          affix: true
        }
      }
    ]
  },
  {
    path: '/tests',
    component: Layouts,
    redirect: '/tests/element-plus',
    name: 'Tests',
    meta: {
      title: 'tests',
      elIcon: 'Grid',
      i18nKey: '测试'
    },
    children: [
      {
        path: 'element-plus',
        component: () => import('@/views/tests/element-plus/index.vue'),
        name: 'ElementPlus',
        meta: {
          title: 'element_ui',
          i18nKey: 'el库',
          keepAlive: true
        }
      },
      {
        path: 'vxe-table',
        component: () => import('@/views/tests/vxe-table/index.vue'),
        name: 'Vxetable',
        meta: {
          title: 'vxe_table',
          i18nKey: 'vxe库',
          keepAlive: true
        }
      },
      {
        path: 'dialog',
        component: () => import('@/views/tests/dialog/index.vue'),
        name: 'Dialog',
        meta: {
          title: 'dialog',
          i18nKey: '弹窗'
        }
      },
      {
        path: 'edit-table',
        component: () => import('@/views/tests/editVxeTable/index.vue'),
        name: 'EditTable',
        meta: {
          title: 'edit_table',
          i18nKey: '编辑表格'
        }
      },
      {
        path: 'item-table',
        component: () => import('@/views/tests/vxeItemTable/index.vue'),
        name: 'ItemTable',
        meta: {
          title: 'item_table',
          i18nKey: '子项表格'
        }
      }
    ]
  },
  {
    path: '/menu',
    component: Layouts,
    redirect: '/menu/menu1',
    name: 'Menu',
    meta: {
      title: 'menu',
      svgIcon: 'menu'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/menu/menu1/index.vue'),
        redirect: '/menu/menu1/menu1-1',
        name: 'Menu1',
        meta: {
          title: 'menu1'
        },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/menu/menu1/menu1-1/index.vue'),
            name: 'Menu1-1',
            meta: {
              title: 'menu1_1',
              keepAlive: true
            }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/menu/menu1/menu1-2/index.vue'),
            redirect: '/menu/menu1/menu1-2/menu1-2-1',
            name: 'Menu1-2',
            meta: {
              title: 'menu1_2'
            },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/menu/menu1/menu1-2/menu1-2-1/index.vue'),
                name: 'Menu1-2-1',
                meta: {
                  title: 'menu1_2_1',
                  keepAlive: true
                }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/menu/menu1/menu1-2/menu1-2-2/index.vue'),
                name: 'Menu1-2-2',
                meta: {
                  title: 'menu1_2_2',
                  keepAlive: true
                }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/menu/menu1/menu1-3/index.vue'),
            name: 'Menu1-3',
            meta: {
              title: 'menu1_3',
              keepAlive: true
            }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/menu/menu2/index.vue'),
        name: 'Menu2',
        meta: {
          title: 'menu2',
          keepAlive: true
        }
      }
    ]
  },

  {
    path: '/about',
    component: Layouts,
    redirect: '/about/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/about/index.vue'),
        name: 'About',
        meta: {
          title: 'about',
          i18nKey: '关于我们',
          elIcon: 'Position'
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes = [
  {
    path: '/permission',
    component: Layouts,
    redirect: '/permission/page',
    name: 'Permission',
    meta: {
      title: 'permission',
      i18nKey: '权限',
      svgIcon: 'lock',
      roles: ['admin', 'editor'], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page.vue'),
        name: 'PagePermission',
        meta: {
          i18nKey: '页面权限',
          title: 'pagePermission',
          roles: ['admin'] // 或者在子导航中设置角色
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive.vue'),
        name: 'DirectivePermission',
        meta: {
          i18nKey: '按钮权限',
          title: 'directivePermission' // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  }
]

const router = createRouter({
  base: '/',
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach(route => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
